package com.owner.lt.tool.utils;

import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC 工具类
 */
@Slf4j
public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     * 文件读取，只会执行一次，使用静态代码块
     */
    static {
        Properties pro = new Properties();
        String filePath = System.getProperty("user.dir") + File.separator + "jdbc.properties";
        try (
                InputStream inputStream = Files.newInputStream(Paths.get(filePath));
        ) {
            // 2.加载文件
            pro.load(inputStream);
        } catch (IOException e) {
            log.error("读取>>>jar同级下的<<<配置文件出错，读取文件位置为{}", filePath);
            log.info("开始读取>>jar内部的<<<配置文件");
            try (
                    InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            ) {
                // 2.加载文件
                pro.load(inputStream);
            } catch (IOException ex) {
                log.error("读取>>jar内部的<<<配置文件出错", e);
            }
        }
        // 3.获取数据
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        password = pro.getProperty("password");
        driver = pro.getProperty("driver");
        // 4.注册驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            log.error("注册驱动失败！", e);
        }
    }

    /**
     * 获取连接
     *
     * @return 连接对象
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    /**
     * 释放资源
     *
     * @param rs
     * @param st
     * @param conn
     */
    public static void close(ResultSet rs, Statement st, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void validateSQL(String sql) {
        try {
            CCJSqlParserUtil.parse(sql);
        } catch (JSQLParserException e) {
            log.error("sql语法错误！", e);
        }
    }
    public static void extracted(Object[] paramList, PreparedStatement preparedStatement) throws SQLException {
        if(paramList != null && paramList.length > 0){
            for (int i = 0; i < paramList.length; i++) {
                if(paramList[i] instanceof String) {
                    preparedStatement.setString(i+1, (String) paramList[i]);
                }else if(paramList[i] instanceof Integer) {
                    preparedStatement.setInt(i+1, (Integer) paramList[i]);
                }else if(paramList[i] instanceof Long) {
                    preparedStatement.setLong(i+1, (Long) paramList[i]);
                }else if(paramList[i] instanceof Double) {
                    preparedStatement.setDouble(i+1, (Double) paramList[i]);
                }else if(paramList[i] instanceof Boolean) {
                    preparedStatement.setBoolean(i+1, (Boolean) paramList[i]);
                }
            }
        }
    }

}

